package com.oot2d.ui;

import com.oot2d.entity.Movable;
import org.newdawn.slick.*;
import com.oot2d.util.InputManager;

public class Menu {

    private int currMenu;
    //Sounds
    private Sound open;
    private Sound close;
    private Sound move;
    private Sound choose;
    //Menu backgrounds
    private SpriteSheet menus;
    //Menu Icons
    private Image itemMenu; //testing
    private Image select;

    private Movable mover;
    private int selected;

    //FINAL VALUES
    //Menu Values
    public static final int ITEM_MENU = 0;
    public static final int EQUIPT_MENU = 1;
    public static final int QUEST_MENU = 2;
    public static final int MAP_MENU = 3;

    private int index = 0;

    public Menu()
    {
        try {
            //Menu Backgrounds
            menus = new SpriteSheet("res/ui/All.png", 480, 320);

            //Default menu to open to
            currMenu = 0;

            //Menu sounds
            open = new Sound("res/music/Pause_Open.ogg");
            close = new Sound("res/music/Pause_Close.ogg");
            move = new Sound("res/music/Pause_Move.ogg");
            choose = new Sound("res/music/Pause_Choose.ogg");

            select = new Image("res/ui/selection.png", false, Image.FILTER_NEAREST);

            //Testing
            itemMenu = new Image("res/ui/inventory.png", false, Image.FILTER_NEAREST);
        } catch (SlickException e) {
            System.err.print(e);
        }

        Items.init();

        mover = new Movable();
    }

    //Play opening animation
    public void openMenu()
    {
        InputManager.clearKeys();
        open.play(1.0f, 0.7f);
        mover.setPos(0.0f, 320.0f);
        mover.moveTo(0.0f, 0.0f, 1.5f);
    }

    //Play closing animation
    public boolean closeMenu()
    {
        return false;
    }

    //Equip an item to a C button
    private void selectItem(int i)
    {
        if (Items.getItem(index) != -1 && Items.getCItemSlot(i) != index) {
            choose.play(1.0f, 0.7f);
            Items.setCItem(index, i);
            HUD.changeItem(index, i);
        }
    }

    public void render(GameContainer gc, Graphics g)
    {
        switch (currMenu) {
            case ITEM_MENU:
                itemMenuRender(g);
                break;
        }
    }

    public void update(GameContainer gc, int d)
    {
        //If the menu is opening or closing, don't take input
        if (mover.isMoving()) {
            mover.update(d);
            return;
        }

        int oldIndex = index;

        if (currMenu == ITEM_MENU && InputManager.rightTap())
            index = (index < 23) ? index + 1 : index;

        if (currMenu == ITEM_MENU && InputManager.leftTap())
            index = (index > 0) ? index - 1 : index;

        if (currMenu == ITEM_MENU && InputManager.upTap())
            index = (index > 5) ? index - 6 : index;

        if (currMenu == ITEM_MENU && InputManager.downTap())
            index = (index < 18) ? index + 6 : index;

        if (currMenu == ITEM_MENU && InputManager.cleft() && !mover.isMoving())
            selectItem(0);

        if (currMenu == ITEM_MENU && InputManager.cdown() && !mover.isMoving())
            selectItem(1);

        if (currMenu == ITEM_MENU && InputManager.cright() && !mover.isMoving())
            selectItem(2);

        if (index != oldIndex)
            move.play();
    }

    //Menu render functions based off inventory flags
    public void itemMenuRender(Graphics g)
    {
        //Playing opening animation
        if (mover.isMoving()) {
            itemMenu.draw(mover.x, mover.y);
            return;
        }

        itemMenu.draw(0,0);

        int h, x;
        for (int i = 0; i < 24; i++) {
            if ((h = Items.getItem(i)) == -1)
                continue;

            Items.drawItem(i, ((i % 6) * 59.0f) + 79.0f, ((i / 6) * 50.0f) + 71.0f);
        }

        //Render selection cursor
        select.draw(((index % 6) * 59.0f) + 72.0f, ((index / 6) * 50.0f) + 64.0f);
    }

    public void equipmentMenu()
    {

    }

    public void mapMenu()
    {

    }

    public void questMenu()
    {

    }

    public void saveMenu()
    {

    }

    public void playOpen()
    {

    }

    public void playClose()
    {
        close.play(1.0f, 0.7f);
    }
}
